#include "prec.h"
#include "artstaticbitmap.h"
#include "artpanel.h"
#include "wx/log.h"
#include "wx/dcbuffer.h"

IMPLEMENT_DYNAMIC_CLASS(ArtStaticBitmap, wxControl)

BEGIN_EVENT_TABLE(ArtStaticBitmap, wxControl)
	EVT_PAINT(ArtStaticBitmap::OnPaint)
	EVT_SIZE(ArtStaticBitmap::OnSize)
	EVT_ERASE_BACKGROUND(ArtStaticBitmap::OnEraseBackground)
END_EVENT_TABLE()

ArtStaticBitmap::ArtStaticBitmap( ArtPanel *parent, wxWindowID id, 
							 const wxBitmap& bitmap, 
							 const wxPoint& pos /*= wxDefaultPosition*/, 
							 const wxSize& size /*= wxDefaultSize*/, 
							 long style /*= 0*/, 
							 const wxString& name /*= wxStaticTextNameStr*/ )
							 : wxControl(parent, id, pos, size, style | wxNO_BORDER, wxDefaultValidator, name)
							 , m_artPanel(parent)
{
	m_bitmap = bitmap;

	InheritAttributes();
	SetInitialSize(size);
}

void ArtStaticBitmap::SetBitmap( const wxBitmap& bitmap )
{
	m_bitmap = bitmap;
	Refresh();
}

void ArtStaticBitmap::OnPaint( wxPaintEvent& event )
{
	wxBufferedPaintDC dc(this);

	dc.SetBackground(wxBrush(GetParent()->GetBackgroundColour()));
	dc.Clear();

	wxBitmap bmp = m_artPanel->GetBackgroundRegion(GetId());
	if (bmp.Ok())
	{
		dc.DrawBitmap(bmp, 0, 0, false);
	}

	if (m_bitmap.Ok())
	{
		wxSize csize = GetClientSize();
		int bx = (csize.x - m_bitmap.GetWidth())/2;
		int by = (csize.y - m_bitmap.GetHeight())/2;

		dc.DrawBitmap(m_bitmap, bx, by, true);
	}
}

void ArtStaticBitmap::OnSize( wxSizeEvent& event )
{
	event.Skip();
	Refresh();
}

void ArtStaticBitmap::OnEraseBackground( wxEraseEvent& event )
{

}

wxSize ArtStaticBitmap::DoGetBestSize() const
{
	return wxSize(m_bitmap.GetWidth(), m_bitmap.GetHeight());
}